package com.action.school;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.action.base.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.pageModel.Json;
import com.pageModel.Major;
import com.service.school.MajorServiceI;

/**
 * 专业ACTION
 * 
 * @author chenq
 * 
 */

@Namespace("/school")
@Action(value = "majorAction")
public class MajorAction extends BaseAction implements ModelDriven<Major>{

	Major major=new Major();
	
	@Override
	public Major getModel() {
		return major;
	}
	
	private MajorServiceI majorService;

	public MajorServiceI getMajorService() {
		return majorService;
	}
	@Autowired
	public void setMajorService(MajorServiceI majorService) {
		this.majorService = majorService;
	}
	
	public void getMajorCmb(){
		super.writeJson(majorService.getMajorCmb(major));
	}
	
	public void datagrid(){
		super.writeJson(majorService.datagrid(major));
	}

	public void remove(){
		majorService.remove(major.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}
	
	public void add() {
		String json = null;
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			boolean state=majorService.add(major);
			if (state) {
				m.put("success", true);
				m.put("msg", "添加成功!");
			} else {
				m.put("success", false);
				m.put("msg", "该专业已存在!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			m.put("success", false);
			m.put("msg", "添加失败！");
			System.out.print(e.getMessage());
		}
		super.writeJson(m);
	}
	
	public void edit() {
		majorService.edit(major);
		Json j=new Json();
		j.setSuccess(true);
		j.setMsg("修改成功！");
		super.writeJson(j);
	}
}
